package com.linran.javaweb.service;

import com.linran.javaweb.annotation.DistributedLock;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Future;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * 在方法参数上使用
 *
 * 注意类名需要加注解@Validated
 * 校验失败会抛出ConstraintViolationException异常
 * 然后我们在全局异常捕获类捕获这个异常，返回给前台对应的错误json
 * @author lin
 * @date 2020-05-10 20:30
 */
@Service
@Validated
public class ValidateService {
    public String testValidate(@Size(min = 1,max = 10,message = "{name.length.error}")String name,
                               @Min(value = 10,message = "年龄最小为10")@Max(value = 100,message = "年龄最大为100")  Integer age,
                               @Future @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss") Date birth){
        return "ok";
    }

    @DistributedLock(bizKey = "key", resourceKey = "source")
    public void aspectAop() {
        System.out.println("target service");
    }
}
